/*!
 * Mobiscroll v2.14.0
 * http://mobiscroll.com
 *
 * Copyright 2010-2014, Acid Media
 * Licensed under the MIT license.
 *
 */
(function ($, undefined) {

    function testProps(props) {
        var i;
        for (i in props) {
            if (mod[props[i]] !== undefined) {
                return true;
            }
        }
        return false;
    }

    function testPrefix() {
        var prefixes = ['Webkit', 'Moz', 'O', 'ms'],
            p;

        for (p in prefixes) {
            if (testProps([prefixes[p] + 'Transform'])) {
                return '-' + prefixes[p].toLowerCase() + '-';
            }
        }
        return '';
    }

    function init(that, options, args) {
        var ret = that;

        // Init
        if (typeof options === 'object') {
            return that.each(function () {
                if (!this.id) {
                    this.id = 'mobiscroll' + (++id);
                }
                if (instances[this.id]) {
                    instances[this.id].destroy();
                }
                new $.mobiscroll.classes[options.component || 'Scroller'](this, options);
            });
        }

        // Method call
        if (typeof options === 'string') {
            that.each(function () {
                var r,
                    inst = instances[this.id];

                if (inst && inst[options]) {
                    r = inst[options].apply(this, Array.prototype.slice.call(args, 1));
                    if (r !== undefined) {
                        ret = r;
                        return false;
                    }
                }
            });
        }

        return ret;
    }

    var id = +new Date(),
        instances = {},
        extend = $.extend,
        mod = document.createElement('modernizr').style,
        has3d = testProps(
            ['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective',
             'msPerspective']),
        hasFlex = testProps(['flex', 'msFlex', 'WebkitBoxDirection']),
        prefix = testPrefix(),
        pr = prefix.replace(/^\-/, '').replace(/\-$/, '').replace('moz', 'Moz');

    $.fn.mobiscroll = function (method) {
        extend(this, $.mobiscroll.components);
        return init(this, method, arguments);
    };

    $.mobiscroll = $.mobiscroll || {
            version: '2.14.0',
            util: {
                prefix: prefix,
                jsPrefix: pr,
                has3d: has3d,
                hasFlex: hasFlex,
                testTouch: function (e, elm) {
                    if (e.type == 'touchstart') {
                        $(elm).attr('data-touch', '1');
                    } else if ($(elm).attr('data-touch')) {
                        $(elm).removeAttr('data-touch');
                        return false;
                    }
                    return true;
                },
                objectToArray: function (obj) {
                    var arr = [],
                        i;

                    for (i in obj) {
                        arr.push(obj[i]);
                    }

                    return arr;
                },
                arrayToObject: function (arr) {
                    var obj = {},
                        i;

                    if (arr) {
                        for (i = 0; i < arr.length; i++) {
                            obj[arr[i]] = arr[i];
                        }
                    }

                    return obj;
                },
                isNumeric: function (a) {
                    return a - parseFloat(a) >= 0;
                },
                isString: function (s) {
                    return typeof s === 'string';
                },
                getCoord: function (e, c) {
                    var ev = e.originalEvent || e;
                    return ev.changedTouches ? ev.changedTouches[0]['page' + c] : e['page' + c];
                },
                getPosition: function (t, vertical) {
                    var style = window.getComputedStyle ? getComputedStyle(t[0]) : t[0].style,
                        matrix,
                        px;

                    if (has3d) {
                        $.each(['t', 'webkitT', 'MozT', 'OT', 'msT'], function (i, v) {
                            if (style[v + 'ransform'] !== undefined) {
                                matrix = style[v + 'ransform'];
                                return false;
                            }
                        });
                        matrix = matrix.split(')')[0].split(', ');
                        px = vertical ? (matrix[13] || matrix[5]) : (matrix[12] || matrix[4]);
                    } else {
                        px = vertical ? style.top.replace('px', '') : style.left.replace('px', '');
                    }

                    return px;
                },
                constrain: function (val, min, max) {
                    return Math.max(min, Math.min(val, max));
                }
            },
            tapped: false,
            presets: {
                scroller: {},
                numpad: {}
            },
            themes: {
                listview: {},
                menustrip: {}
            },
            i18n: {},
            instances: instances,
            classes: {},
            components: {},
            defaults: {
                theme: 'mobiscroll',
                context: 'body'
            },
            userdef: {},
            setDefaults: function (o) {
                extend(this.userdef, o);
            },
            presetShort: function (name, c, p) {
                this.components[name] = function (s) {
                    return init(this,
                                extend(s, {component: c, preset: p === false ? undefined : name}),
                                arguments);
                };
            }
        };

})(jQuery);
